package cn.myfirefly.moming.integrate.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.myfirefly.moming.common.model.DepartmentDto;
import cn.myfirefly.moming.common.model.StaffDto;
import cn.myfirefly.moming.common.model.StaffFilterDto;
import cn.myfirefly.moming.common.service.DepartmentService;
import cn.myfirefly.core.BaseController;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;

@Controller
@RequestMapping("department")
public class DepartmentController extends BaseController {

	@Autowired
	private DepartmentService departmentService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("departments", departmentService.getAll());
		return "department/index";
	}
	
	@RequestMapping("add.do")
	@ResponseBody
	public ResultDto add(DepartmentDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		warpModifyInfo(dto, request);
		String msg = departmentService.save(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("新增成功", dto.getDepartmentSn().toString());
	}
	
	@RequestMapping("del.do")
	@ResponseBody
	public ResultDto del(DepartmentDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = departmentService.del(dto.getDepartmentSn());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
	
	@RequestMapping("getDepartment.do")
	public String getDepartment(DepartmentDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("department", departmentService.getDepartment(dto.getDepartmentSn()));
		return "department/department-detail";
	}
	
	@RequestMapping("update.do")
	@ResponseBody
	public ResultDto update(DepartmentDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		warpModifyInfo(dto, request);
		String msg = departmentService.update(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("保存成功");
	}
	
	@RequestMapping("updatepid.do")
	@ResponseBody
	public ResultDto updatePId(DepartmentDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		warpModifyInfo(dto, request);
		String msg = departmentService.updatePId(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("保存成功");
	}
	
	@RequestMapping("stafflist.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto<StaffDto> staffList(StaffFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		PageResultDto<StaffDto> result = departmentService.getStaffList(dto, 
				Integer.valueOf(request.getParameter("departmentSn")));
		return result;
	}
	
	@RequestMapping("toDepartmentLookUp.do")
	public String toDepartmentLookUp(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("departments", departmentService.getAll());
		return "staff/department-lookup";
	}
}
